﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OT.Data;
using OT.Areas.Student.Models;
using PagedList;

namespace OT.Areas.Student.Controllers
{
    public class AttemptDtlController : Controller
    {
        #region Declaration
        readonly OTEntitiesme OTDb = new OTEntitiesme();
        int LanguageID = Utilities.GetLanguageID();
        double TotalMarks = 0;
        #endregion

        #region Index
        public ActionResult Index(int AttemptID)
        {
           
                var data = OTDb.sp_EndTest(AttemptID).ToList<sp_EndTest_Result>();
                double TotalScore = (int)data[0].TotalScore;
                double TotalMarks = (int)data[0].TotalMarks;
                double Percantage = Convert.ToDouble((Math.Round((TotalScore / TotalMarks) * 100, 2)));
                var Result = (Percantage >= data[0].PassingScore) ? "Passed" : "Failed";
                ViewBag.PassingScore = data[0].PassingScore;
                ViewBag.TotalQuestion = data[0].TotalQuestion;
                ViewBag.TotalMarks = data[0].TotalMarks;
                ViewBag.AttemptedQuestions = data[0].AttemptedQuestions;
                ViewBag.UnAttemptedQuestions = data[0].UnAttemptedQuestions;
                ViewBag.CorrectAnswers = data[0].CorrectAnswers;
                ViewBag.WrongAnswers = data[0].WrongAnswers;
                ViewBag.totalPositiveMarks = data[0].totalPositiveMarks;
                ViewBag.totalNegativeMarks = data[0].totalNegativeMarks;
                ViewBag.TotalScore = data[0].TotalScore;
                if (Percantage > 0)
                {
                    ViewBag.Percantage = Math.Round(Percantage, 2);
                }
                else
                {
                    ViewBag.Percantage = "";
                }
                ViewBag.Result = Result;

                ViewBag.TotalMarks = data[0].totalPositiveMarks - data[0].totalNegativeMarks;
                ViewBag.Outof = TotalMarks.ToString();
          
            var Attempts = (List<AttemptDtls>)null;
            try
            {
                Attempts = (from c in OTDb.AttemptDtls
                            where c.AttemptID.Value.Equals(AttemptID)
                            join top in OTDb.Questions
                        on c.QuestionID equals top.QuestionID
                            select new AttemptDtls
                            {
                                QuestionID = c.QuestionID.Value,
                                QuestionName = top.QuestionName,
                                AttemptID = c.AttemptID.Value,
                                YourAnswer = (byte?)c.YourAnswer.Value??0,
                                yoursAns = (c.YourAnswer.Value == 1) ? c.Question.Answer1 : (c.YourAnswer.Value == 2) ? c.Question.Answer2 : (c.YourAnswer.Value == 3) ? c.Question.Answer3 :(c.YourAnswer.Value == 4)? c.Question.Answer4:"No Anser Given",
                                CorrectAnswer = (byte?)c.CorrectAnswer.Value??0,
                                CorrectAns = (c.CorrectAnswer.Value == 1) ? c.Question.Answer1 : (c.CorrectAnswer.Value == 2) ? c.Question.Answer2 : (c.CorrectAnswer.Value == 3) ? c.Question.Answer3 : (c.CorrectAnswer.Value == 4) ? c.Question.Answer4 : "No Anser Given",
                                IsRight =(bool?)c.IsRight.Value??false,
                                Marks =(double?) c.Marks.Value??0,
                            }).ToList();

                if (Attempts != null)
                {
                    foreach (AttemptDtls att in Attempts)
                    {
                        TotalMarks += att.Marks;
                    }
                }

                //mark = OTDb.AttemptMsts.Where(a => a.AttemptID.Equals(AttemptID)).Select(t => t.Test.PositiveMark.Value).FirstOrDefault();
                //totque = OTDb.TestQuestions.Where(t => t.TestID.Value.Equals(OTDb.AttemptMsts.Where(a => a.AttemptID.Equals(AttemptID)).FirstOrDefault().TestID.Value)).Count<TestQuestion>();
                //ViewBag.TotalMarks = TotalMarks.ToString();
                //ViewBag.Outof = (totque * mark).ToString();

            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }

            return View(Attempts.ToPagedList(1, 300));

        }
        #endregion

        #region EndTest
        public ActionResult EndTest()
        {
            return View();
        }
        #endregion

        #region Result
        public ActionResult Result(long AttemptID)
        {
            TestResult trslt = new TestResult();
            try
            {
                trslt.AttemptedQuestions = OTDb.AttemptDtls.Where(a => a.AttemptID.Value.Equals(AttemptID)).Count<AttemptDtl>();
                trslt.PassingScore = OTDb.AttemptMsts.Where(a => a.AttemptID.Equals(AttemptID)).Select(t => t.Test.PassingScore.Value).FirstOrDefault();
                trslt.PositiveMark = OTDb.AttemptMsts.Where(a => a.AttemptID.Equals(AttemptID)).Select(t => t.Test.PositiveMark.Value).FirstOrDefault();
                trslt.NegativeMark = OTDb.AttemptMsts.Where(a => a.AttemptID.Equals(AttemptID)).Select(t => t.Test.NegativeMark.Value).FirstOrDefault();
                trslt.TotalQuestion = OTDb.TestQuestions.Where(t => t.TestID.Value.Equals(OTDb.AttemptMsts.Where(a => a.AttemptID.Equals(AttemptID)).FirstOrDefault().TestID.Value)).Count<TestQuestion>();
                trslt.UnAttemptedQuestions = trslt.TotalQuestion - trslt.AttemptedQuestions;
                trslt.TotalMarks = trslt.TotalQuestion * trslt.PositiveMark;
                trslt.CorrectAnswers = OTDb.AttemptDtls.Where(a => a.AttemptID.Value.Equals(AttemptID) && a.IsRight.Value == true).Count<AttemptDtl>();
                trslt.WrongAnswers = OTDb.AttemptDtls.Where(a => a.AttemptID.Value.Equals(AttemptID) && a.IsRight.Value == false).Count<AttemptDtl>();
                float totalpos = trslt.PositiveMark * trslt.CorrectAnswers;
                float totalneg = trslt.NegativeMark * trslt.WrongAnswers;
                trslt.TotalScore = totalpos - totalneg;
                trslt.SubjectName = OTDb.AttemptMsts.Where(a => a.AttemptID.Equals(AttemptID)).FirstOrDefault().Test.Category.Subjects.FirstOrDefault().SubjectName;
                trslt.TestName = OTDb.AttemptMsts.Where(a => a.AttemptID.Equals(AttemptID)).FirstOrDefault().Test.TestName;
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return View(trslt);

        }
        #endregion
    }
}
